package com.onedec.admin.controller;

import com.onedec.admin.entity.OnedecEmp;
import com.onedec.admin.service.AdminService;
import com.onedec.utils.ResponseEntity;

import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.SQLOutput;
import java.util.List;
import java.util.Properties;

@RestController
@Slf4j
@RefreshScope
public class AdminController {
    private OnedecEmp onedecEmp;
    @Autowired
    private AdminService adminService;

    @PostMapping("login")
    public ResponseEntity findBylogin(String empUsername, String empPwd, HttpServletRequest request) {
        int sum = 0;
        System.out.println("用户名" + empUsername + "密码" + empPwd);
        OnedecEmp emp = adminService.findByUser(empUsername);
        System.out.println(emp);
        try {
            if (emp != null || !"".equals(emp)) {
                if (emp.getEmpPwd().equals(empPwd)) {
                    HttpSession session = request.getSession();
                    session.setAttribute("emp", emp);
                    return ResponseEntity.SUCCESS;
                }
            }
        } catch (Exception e) {
            return new ResponseEntity("404", "密码错误或用户不存在");
        }
        return new ResponseEntity("404", "密码错误或用户不存在");
    }

    @GetMapping("/get")
    public ResponseEntity findById(int empId) {
        System.out.println("这里就是id" + empId);
        OnedecEmp emp = adminService.findById(empId);
        return new ResponseEntity(emp);
    }

    /**
     * 通过邮箱找回密码
     *
     * @param email
     * @return
     */
    @GetMapping("email")
    public ResponseEntity findByemail(String email, int i) {
        OnedecEmp emp = adminService.findByEmail(email);
        onedecEmp = emp;
        System.out.println(emp);
        System.out.println("传进来的email" + email);
        if (emp != null) {
            System.out.println(i);
            if (i == 1) {
                AdminController.sendMail(email);
                return ResponseEntity.SUCCESS;
            }
            System.out.println("Email" + email);
            return ResponseEntity.SUCCESS;
        }
        return new ResponseEntity("404", "该邮箱未注册账号");
    }
    /**
     * 查询所有用户
     */
    @GetMapping("selectAllEmp")
    public ResponseEntity selectAllEmp(){
        List<OnedecEmp> list = adminService.selectAllEmp();
        return new ResponseEntity(list);
    }

    /**
     * 修改密码
     *
     * @param empPwd
     * @param empPwd1
     * @return
     */
    @PostMapping("updatePwd")
    public ResponseEntity updatePwd(String empPwd, String empPwd1) {
        System.out.println("1" + empPwd + "2" + empPwd1);
        if (!empPwd.equals(empPwd1)) {
            System.out.println("两次密码不相同请重新输入");
            return new ResponseEntity("404", "两次密码不相同请重新输入");

        } else {
            System.out.println("修改成功");
            onedecEmp.setEmpPwd(empPwd);
            int i = adminService.updatePwd(onedecEmp);
            if (i > 0) {
                return ResponseEntity.SUCCESS;
            }
        }
        return new ResponseEntity("404", "两次密码不相同请重新输入");
    }

    /**
     * redis sesion共享
     *
     * @param request
     * @return
     */
    @GetMapping("/getUser")
    public void getUser(HttpServletRequest request) {
        OnedecEmp emp = (OnedecEmp) request.getSession().getAttribute("emp");
        System.out.println(emp);
    }

    /**
     * 邮件发送
     *
     * @param email
     */
    public static void sendMail(String email) {
        try {
            final Properties props = new Properties();
            props.put("mail.user", "1959546143@qq.com");
            props.put("mail.password", "fgcssaoxatveeeib"); // 授权码
            props.put("mail.smtp.auth", "true");
            props.put("mail.transport.protocol", "smtp");
            props.put("mail.smtp.host", "smtp.qq.com");
            props.put("mail.smtp.port", "25");

            Session mailSession = Session.getDefaultInstance(props);

            Message msg = new MimeMessage(mailSession);
            msg.setFrom(new InternetAddress("1959546143@qq.com"));  //发件人
            msg.addRecipients(Message.RecipientType.TO, InternetAddress.parse(email)); //收件人
            msg.setSubject("用户密码找回邮件-壹装修"); //主题
            msg.setContent("<h1>此邮件为壹装修找回密码邮件！请点击下面链接完成重置密码操作！</h1><br />" + "此邮件30分钟有效!"
                    + "<h4><a href='http://localhost：9001/updatepwd.html?email=" + email
                    + "'>href='http://localhost：9001/updatepwd.html?email=" + email
                    + "</a></h4>", "text/html;charset=UTF-8");
            msg.saveChanges();
            Transport transport = mailSession.getTransport("smtp");
            transport.connect(props.getProperty("mail.smtp.host"), props.getProperty("mail.user"),
                    props.getProperty("mail.password"));
            transport.sendMessage(msg, msg.getAllRecipients());
            transport.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @GetMapping("current")
    public ResponseEntity selectCurrentEmp(HttpServletRequest request) {
        final Object emp = request.getSession().getAttribute("emp");
        return new ResponseEntity(emp);

    }

    @GetMapping("emp/designer")
    public ResponseEntity selectDesigner(Integer page,Integer limit) {
        final List<OnedecEmp> emps = adminService.selectDesigner();
        System.out.println(emps);
        return new ResponseEntity(emps);
    }
    @GetMapping("emp/manager")
    public ResponseEntity selectManager(Integer page,Integer limit) {
        final List<OnedecEmp> emps = adminService.selectManager();
        System.out.println(emps);
        return new ResponseEntity(emps);
    }
    @GetMapping("emp/supervision")
    public ResponseEntity selectSupervision(Integer page,Integer limit) {
        final List<OnedecEmp> emps = adminService.selectSupervision();
        System.out.println(emps);
        return new ResponseEntity(emps);
    }
}
